package com.zhiwang.planet.ui.my

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.event.LoginEvent
import com.zhiwang.planet.mvp.p.UserPresenter
import com.zhiwang.planet.mvp.v.BindWxView
import com.zhiwang.planet.mvp.v.WxLoginView
import com.zhiwang.planet.mvvm.model.entity.User
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.util.AppHelper
import com.zhiwang.planet.util.Toaster
import com.zhiwang.planet.util.suicideAfterTransition
import kotlinx.android.synthetic.main.activity_wx_old_user_bind.*
import org.greenrobot.eventbus.EventBus
import javax.inject.Inject

class WxOldUserBindActivity : BaseActivity(), BindWxView, WxLoginView {


    companion object SOUL {
        fun start(context: Context, openid: String) {
            val intent = Intent(context, WxOldUserBindActivity::class.java)
            intent.putExtra("Openid", openid)
            context.startActivity(intent)
        }
    }

    @Inject
    lateinit var userPresenter: UserPresenter

    lateinit var openid :String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wx_old_user_bind)
        setSupportActionBar(toolbar)
        observeTextChangeWithTargetButtonEnableStatus(arrayOf(et_phone, et_password), confirmBindBtn)
        openid = intent.getStringExtra("Openid")
    }

    override fun initInjection() {
        super.initInjection()
        DaggerUserComponent.builder()
                .appComponent(getAppComponent())
                .accountModule(AccountModule(this))
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)

        userPresenter.bindWxView = this
        userPresenter.wxLoginView = this
    }

    fun bind(v: View) {

        val phone = et_phone.text.toString()
        val password = et_password.text.toString()

        when {
            TextUtils.isEmpty(phone) -> Toaster.message("手机号不能为空")
            TextUtils.isEmpty(password) -> Toaster.message("密码不能为空")

            else -> {
                val deviceid = AppHelper.getDeviceIdWithoutGuarantee(this)
                userPresenter.bindWxOldUser(phone, password,/*openid*/openid,deviceid)
            }
        }
    }


    override fun onWxBind() {
//        userPresenter.getUserInfo()
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return super.onSupportNavigateUp()
    }

    override fun onLogin(user: User) {
        Toaster.message(R.string.login_success)
        EventBus.getDefault().post(LoginEvent())
        suicideAfterTransition()
    }
}
